﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using Authority.Services;
using Authority.Entity;
using DoNet.FrameWork.DomainModel;
using DoNet.FrameWork.Repository.Specification;
using MvcApp.Models;
using MvcApp.CommonSupport;
using MvcApp.CommonSupport.Filter;

namespace MvcApp.Controllers
{
    public class UserController : Controller
    {
        private readonly IPostService _postService;
        private readonly IOrganizeService _organizeService;
        private readonly IUserService _userService;

        public UserController(IOrganizeService organizeService, IPostService postService, IUserService userService)
        {
            _organizeService = organizeService;
            _postService = postService;
            _userService = userService;
        }

        [AuthorizeFilter(ModuleName = "001003", PermisionType = 1)]
        public ActionResult Index()
        {
            string organizeJson = "[";
            List<Organize> organizes = _organizeService.GetList();
            foreach(Organize o in organizes)
            {
                organizeJson += "{'Id':'" + o.Id + "','OrganizeName':'"+ o.OrganizeName +"'},";
            }
            organizeJson = organizeJson.Trim(',');
            organizeJson += "]";
            ViewBag.organizeJson = organizeJson;

            ISpecification<Post> condition = new DirectSpecification<Post>(p => true);
            Expression<Func<Post, string>> orderByExpression = p => p.Code;
            List<Post> posts = _postService.GetList<string>(condition, orderByExpression, false);
            string postJoson = "[";
            foreach (var p in posts)
            {
                postJoson += "{'Id':'" + p.Id + "','OrganizeId':'" + p.OrganizeId + "','PostName':'" + p.PostName + "'},";
            }
            postJoson = postJoson.Trim(',');
            postJoson += "]";
            ViewBag.postJoson = postJoson;

            return View();
        }

        public JsonResult ALLUserJson(int page, int rows)
        {
            GridData<Authority.Entity.User> userGd = new GridData<Authority.Entity.User>();
            ISpecification<Authority.Entity.User> condition = new DirectSpecification<Authority.Entity.User>(u => true);
            Expression<Func<Authority.Entity.User, DateTime>> orderByExpression = u => u.SysCreatedOn;
            PageData<Authority.Entity.User> userPd = _userService.FindAll<DateTime>(page, rows, condition, orderByExpression, false);
            userGd.total = userPd.TotalCount;
            userGd.rows = userPd.DataList;
            return Json(userGd);
        }

        [AuthorizeFilter(ModuleName = "001003", PermisionType = 2)]
        public ActionResult InsertOrEditUser(User user)
        {
            CurrentUser currentUser = new CurrentUser();
            user.SysCreatedBy = currentUser.UserInfo.Id;
            user.SysCreatedOn = DateTime.Now;
            user.SysModifiedBy = currentUser.UserInfo.Id;
            user.SysModifiedOn = DateTime.Now;
            string rtn = _userService.InsertOrEditUser(user);
            return Content(rtn);
        }

        [AuthorizeFilter(ModuleName = "001003", PermisionType = 4)]
        public ActionResult DeleteUserById(string id)
        {
           string rtn = _userService.DeleteUserById(new Guid(id));
           return Content(rtn);
        }

        public ActionResult ReSetPassword(string id)
        {
            string rtn = _userService.ReSetPasswordById(new Guid(id));
            return Content(rtn);
        }
    }
}
